<?php
/**
 * Created by PhpStorm.
 * User: Inhere
 * Date: 15-4-1
 * Time: 上午10:08
 * Used: bootstrap 导航栏
 * file: Navbar.php
 */

namespace ulue\ext\zui;

use ulue\core\router;
use ulue\libs\front\Element;

class Navbar extends Nav
{

///////////////////////// tag nav /////////////////////////

    /**
     * navbar-default 默认导航条
     * navbar-inverse 深色导航条
     * 不添加以上的样式 则是最简单的样式，便于进行定制。
     *===
     * 固定导航条
     * navbar-fixed-top  固定到顶部
     * navbar-fixed-bottom 固定到底部
     * @var array
     */
    public $attrs = [
        'class' => 'navbar navbar-default',
        'role'  => 'navigation',
    ];

///////////////////////// tag nav>div.nav-header /////////////////////////

    /**
     * @var array
     */
    public $navHeader = [];

///////////////////////// tag nav>div.navbar-collapse /////////////////////////

    /**
     * @var array
     */
    public $navbarDivAttrs = [
        'class' => 'collapse navbar-collapse navbar-collapse-example',
        'id'    => 'ulue-app-navbar' // 必须与 navHeader --> 'button' => ['data-target'=>'#ulue-app-navbar'] 值对应
    ];

///////////////////////// tag nav>div.navbar-collapse>ul.nav.navbar-nav /////////////////////////

    // 主导航列表
    /**
     * @var array
     */
    public $mainNav = [];

    /*
     *  可用的追加样式
     * nav-justified -- 自适应宽度
     * nav-primary -- 主要导航
     * nav-secondary -- 次要导航，样式简单
     * nav-pills -- 圆点导航
     */
    /**
     * @var array
     */
    public $mainNavAttrs = ['class' => 'nav navbar-nav'];

///////////////////////// search box: tag nav>div.navbar-collapse>form.navbar-form  /////////////////////////

    /**
     * @var array
     */
    public $searchBox = [];

    /**
     * @var array
     */
    public $searchBoxAttrs = [
        'method' => 'get',
        'action' => '',
        'class'  => 'navbar-form navbar-left',
        'role'   => 'search'
    ];

///////////////////////// right nav: tag nav>div.navbar-collapse>ul.navbar-right /////////////////////////

    /**
     * @var array
     */
    public $rightNav = [];
    /**
     * @var array
     */
    public $rightNavAttrs = ['class' => 'nav navbar-nav navbar-right'];

    /**
     *
     */
    public function __construct()
    {
        $this->init();

        $this->navHeader = $this->navHeader();
        $this->mainNav    = $this->mainNav();
        $this->searchBox = $this->searchBox();
        $this->rightNav  = $this->rightNav();
        // add custom ...
    }

    /**
     * @return string
     */
    public function beginExecute()
    {
        // tag nav
        $nav = new Element('nav',null,$this->attrs);

        // tag nav>div.nav-header
        if ( $headerDiv = $this->_handleNavHeader() ) {
            $nav->addChild($headerDiv);
        }

        // tag nav>div.navbar-collapse
        $navbarDiv = $nav->addChild('div',null, $this->navbarDivAttrs);
        $mainContent = '';

        // assembly Parts
        foreach ($this->assemblyParts() as $callback) {
            if (method_exists($this, $callback)) {
                $mainContent .= $this->$callback();
            }
        }

        $navbarDiv->setContent($mainContent);

        return $nav->getString();
        // return $this->render('navbar');
    }

    /**
     * 设定主导航各部分组装顺序
     * 放置各部分的元素创建处理方法，将会按顺序调用组装
     */
    protected function assemblyParts()
    {
        return ['_handleMainNav','_handleSearchBox','_handleRightNav',];
    }

/////////////////////////////////// habdle each type config, generate html element ///////////////////////////////////
    /**
     * @return null|Element
     */
    protected function _handleNavHeader()
    {
        $div = null;

        if ($header = $this->navHeader) {
            $div = new Element('div', null, $header['attrs']);

            $buttonText = <<<EOF
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
EOF;
            $div->addChild('button',$buttonText, $header['button']);
            $brand = $div->addChild('a');
            $brandAttr = $header['brand'];

            $brandAttr['href'] = empty($brandAttr['href']) ? 'javascript:;' : $brandAttr['href'];

            if (isset($brandAttr['img'])) {
                $brand->addChild('img', null, ['src'=>$brandAttr['img']]);
                unset($brandAttr['img']);
            // } else {
            //     $brand->setContent($brandAttr['text']);
            }

            $brand->setAttrs($brandAttr);
        }

        return $div;
    }

    //strat: tag nav>div.navbar-collapse>ul.nav.navbar-nav
    protected function _handleMainNav()
    {
        $mainNav = '';

        if ($this->mainNav) {
            $mainNav = $this->createUlElement($this->mainNav, $this->mainNavAttrs);
        }

        return (string)$mainNav;
    }

    /**
     * 2. search box: tag nav>div.navbar-collapse>form.navbar-form
     * @return null|Element
     */
    protected function _handleSearchBox()
    {
        $form = null;

        if ($searchBox = $this->searchBox) {
            $form   = new Element('form', null, $searchBox['attrs']);

            $div    = $form->addChild('div', null, ['class'=>'form-group']);

            $input  = $div->addChild('input', null,  $searchBox['input']);

            $button = $form->addChild('button', null,  $searchBox['button']);
        }

        return $form;
    }

    // add custom ...
    // protected function _handleCustomTag()
    // {
    //     if ( $customTag = $this->customTag ) {
    //           new Element(...);
    //     }
    // }

    // end: right nav: tag nav>div.navbar-collapse>ul.navbar-right
    protected function _handleRightNav()
    {
        $rightNav = '';

        if ( $this->rightNav ) {
            $rightNav = $this->createUlElement($this->rightNav, $this->rightNavAttrs);
        }

        return (string)$rightNav;
    }


/////////////////////////////////// config data  ///////////////////////////////////

    /**
     * @return array
     */
    protected function navHeader()
    {
        return [
            /*
              <!-- Brand and toggle get grouped for better mobile display -->
              <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#ulue-app-navbar">
                  <span class="sr-only">Toggle navigation</span>
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Brand</a>
              </div>
            导航栏 Brand(可选).  url -- 点击时跳转地址，默认首页，为空不跳转
            */
            'brand'  => ['text' => 'myBrand', 'href' => '/' , 'class' => 'navbar-brand'],
            //'brand' => ['img'=>'xxx/logo.png', 'href' => '/' , 'class' => 'navbar-brand'],// 使用图片
            'attrs'  => ['class'=>"navbar-header"],
            'button' => [
                'type'        => "button",
                'class'       => "navbar-toggle",
                'data-toggle' => "collapse",
                'data-target' => "#ulue-app-navbar"
            ]
        ];
    }

    /**
     * @return array
     */
    protected function mainNav()
    {
        //  setting
        return [
           /*
            <li><a href="###"><i class="icon-user"></i> 个人资料 <span class="label label-badge label-success">4</span></a></li>
            // route 当前标签的路由字符串，用于匹配 栏目高亮  e.g. '/post/manage'
            'route'    => [
                'text'    => '',  // 文本内容
                'href'    => '' , // 为空时 自动设置 为 javascript:;
                'icon'    => 'icon-home', // 添加图标
                'append'  => '<span class="label label-badge label-success">4</span>',// 在 text 之后追加元素

                // 给 a 链接 添加其他标签属性 e.g. ['title'=>'a title', 'id'=>'ddd', 'data-mark'=>'xxx']
                'attrs' => [],
                'submenu' => [],// ... 设置跟父级类似
            ],
            */

            'index'    => [
                'text'  => ' 首页',
                'href'  => \Ioc::get('app.uri')->get('index.path'),
                'icon'  => 'icon-home',
                'attrs' => [],
            ],
            'site/index'    => [
                'text'  => ' 站点',
                'href'  => '' ,
                'icon'  => 'icon-inbox',
                // 'attrs' => [],
                'submenu' => [
                    'site/list' => [
                        'text'  => ' 列表',
                        'href'  => router\Uri::to('site/list') ,
                        'icon'  => 'icon-list',
                    ],
                    'site/add' => [
                        'text'  => ' 添加',
                        'href'  => router\Uri::to('site/add') ,
                        'icon'  => 'icon-plus',
                    ],
                ],
            ],
            'site/about'=> ['text' => ' 关于', 'href' => router\Uri::to('site/about') , 'icon' => 'icon-chat' ],
        ];
    }

    /**
     * 搜索框
     *
     */
    protected function searchBox()
    {
        //  setting
        return [
        /*
        <!-- search box -->
        <form class="navbar-form navbar-left" role="search">
          <div class="form-group">
            <input type="text" class="form-control" placeholder="Search">
          </div>
          <button type="submit" class="btn btn-default">Submit</button>
        </form>
        */  'attrs'  => [
                'method' => 'get',
                'action' => '',
                'class'  => 'navbar-form navbar-left',
                'role'   => 'search'
            ],
            'input'  => [ 'type' => 'text', 'class' => 'form-control', 'placeholder' => 'Search ... ...'],
            'button' => [ 'text' => 'Search', 'type' => 'submit', 'class' => 'btn btn-default']
        ];
    }

    /**
     * 右侧导航
     * 配置数据跟 mainNav() 一样
     */
    protected function rightNav()
    {
        //  setting
        return [
            'index'    => [
                'text'  => ' 链接',
                'href'  => \Ioc::get('app.uri')->get('index.path'),
                'icon'  => 'icon-home',
                'attrs' => [],
            ],
            'site/more'    => [
                'text'  => ' 更多',
                'href'  => '' ,
                'icon'  => 'icon-inbox',
                // 'attrs' => [],
                'submenu' => [
                    [
                        'text'  => 'GIT',
                        'href'  => 'http://www.higit.com/',
                        'icon'  => 'icon-stack',
                        'attrs'  => ['target'=>'_blank'],
                    ],
                    [
                        'text'  => 'FileManager',
                        'href'  => 'http://www.kode.com/',
                        'icon'  => 'icon-archive',
                        'attrs'  => ['target'=>'_blank'],
                    ],
                ],
            ],
        ];
    }

    /**
     * 自定义部分
     *
     */
    protected function customTag()
    {
        # code...
    }

    /**
     * @param Element $tag
     * @param $route
     * @param $value
     * @return bool
     */
    protected function specialHandleTag(Element $tag, $route, $value)
    {
        // special -- nav Title
        // if ( trim($mark)==='navTitle' ) {
        //     $tag->setClass('nav-heading');

        //     return true;
        // }

        return false;
    }


}// end class Navbar